package file.day9_20.day06.服务端和客户端多线程01;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private Socket socket;

    public Client() {
        System.out.println("正在连接");

        try {
            socket = new Socket("localhost", 8091);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("连接成功");
    }

    public void start() {


        try (
                PrintWriter pw = new PrintWriter(
                        new BufferedWriter(
                                new OutputStreamWriter(
                                        socket.getOutputStream(), "UTF-8"
                                )
                        ),true
                );
        ) {
            Scanner scanner = new Scanner(System.in);
            while (true){
                System.out.println("请输入要发送的内容：");
                String line = scanner.nextLine();
                if(line.equals("exit"))break;
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
